<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" v-if="!inSide">
      <el-form-item label="吸毒人员" prop="userId">
        <user-drug-select v-model="queryParams.userId" :onchange="handleQuery" placeholder="请选择吸毒人员" :types="['drug_sjsk']"/>
      </el-form-item>
      <el-form-item label="检测人" prop="jcr">
        <el-input
          v-model="queryParams.jcr"
          placeholder="请输入检测人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测类型" prop="jclx">
        <el-select v-model="queryParams.jclx" placeholder="请选择检测类型" clearable>
          <el-option
            v-for="dict in dict.type.jclx"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检测方式" prop="jcfs">
        <el-select v-model="queryParams.jcfs" placeholder="请选择检测方式" clearable>
          <el-option
            v-for="dict in dict.type.jcfs"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检测日期" prop="jcrq">
        <el-date-picker clearable
          v-model="queryParams.jcrq"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择检测日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检测结果" prop="jcjg">
        <el-select v-model="queryParams.jcjg" placeholder="请选择检测结果" clearable>
          <el-option
            v-for="dict in dict.type.jcjg"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.shzt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="!inSide">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['event:jcjh:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['event:jcjh:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['event:jcjh:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['event:jcjh:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jcjhList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="!inSide"/>
      <el-table-column label="id" align="center" prop="jcjhId" v-if="false"/>
      <el-table-column label="吸毒人员" align="center" prop="userInfo" v-if="!inSide"/>
      <el-table-column label="代检测人员" align="center" prop="memberInfo" v-if="!inSide"/>
      <el-table-column label="检测人" align="center" prop="jcr" v-if="!inSide"/>
      <el-table-column label="检测类型" align="center" prop="jclx" v-if="!inSide">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jclx" :value="scope.row.jclx"/>
        </template>
      </el-table-column>
      <el-table-column label="检测方式" align="center" prop="jcfs" v-if="!inSide">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jcfs" :value="scope.row.jcfs"/>
        </template>
      </el-table-column>
      <el-table-column label="检测周期" align="center" prop="cycleRange" width="230" v-if="inSide"></el-table-column>
      <el-table-column label="检测日期" align="center" prop="jcrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jcrq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结果" align="center" prop="jcjg">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jcjg" :value="scope.row.jcjg"/>
        </template>
      </el-table-column>
      <el-table-column label="检测地点" align="center" prop="jcdd" show-overflow-tooltip v-if="!inSide"/>
      <el-table-column label="审批人" align="center" prop="sprInfo" v-if="!inSide"/>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shzt" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['event:jcjh:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['event:jcjh:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检测计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="吸毒人员" prop="userId">
              <user-drug-select v-model="form.userId" placeholder="请选择吸毒人员" :onchange="handleUserChange" :types="['drug_sjsk', 'drug_zdgz']" :readonly="inSide"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代检测工作人员" prop="memberId">
              <user-member-select v-model="form.memberId" placeholder="请选择工作人员" :onchange="handleUserChange" :readonly="inSide"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测人" prop="jcr">
              <el-input v-model="form.jcr" placeholder="请输入检测人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测类型" prop="jclx">
              <el-select v-model="form.jclx" placeholder="请选择检测类型">
                <el-option
                  v-for="dict in dict.type.jclx"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测方式" prop="jcfs">
              <el-select v-model="form.jcfs" placeholder="请选择检测方式">
                <el-option
                  v-for="dict in dict.type.jcfs"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测周期" prop="cycleRange">
              <el-select v-model="form.cycleRange" placeholder="请选择检测周期">
                <el-option :value="plan.rangeStr" :label="plan.rangeStr" v-for="plan in plans" :key="plan.rangeStr">
                  {{plan.startDate}} - {{plan.endDate}}
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测日期" prop="jcrq">
              <el-date-picker clearable
                              v-model="form.jcrq"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择检测日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测结果" prop="jcjg">
              <el-select v-model="form.jcjg" placeholder="请选择检测结果">
                <el-option
                  v-for="dict in dict.type.jcjg"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="检测地点" prop="jcdd">
              <el-input v-model="form.jcdd" placeholder="可通过点击地图获取详细地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经度" prop="lng">
              <el-input v-model="form.lng" placeholder="可通过点击地图获取经度" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="lat">
              <el-input v-model="form.lat" placeholder="可通过点击地图获取纬度" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="位置" prop="pos">
              <map-picker v-model="currentPos" @detail-address="getDetailAddress" v-if="open"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="检测报告" prop="jcbg">
              <file-upload v-model="form.jcbg" :file-type="['pdf','doc', 'docx']"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="视频上传" prop="sp">
              <file-upload v-model="form.sp" :file-type="['mp4']"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="附件" prop="fj">
              <file-upload v-model="form.fj" :file-type="['pdf','doc', 'docx']"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人" prop="spr">
              <user-member-select v-model="form.spr" placeholder="请选择审批人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择审核状态">
                <el-option
                  v-for="dict in dict.type.shzt"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listJcjh, getJcjh, delJcjh, addJcjh, updateJcjh, getJcPlan } from "@/api/event/jcjh";
import sideView from "@/views/event/mixin/sideView";

export default {
  name: "Jcjh",
  dicts: ['shzt', 'jclx', 'jcfs', 'jcjg'],
  mixins: [sideView],
  data() {
    return {
      plans: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检测计划表格数据
      jcjhList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        jcr: undefined,
        jclx: undefined,
        jcfs: undefined,
        jcrq: undefined,
        jcjg: undefined,
        jcdd: undefined,
        jcbg: undefined,
        sp: undefined,
        fj: undefined,
        lat: undefined,
        lng: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "吸毒人员id不能为空", trigger: "blur" }
        ],
        jcr: [
          { required: true, message: "检测人不能为空", trigger: "blur" }
        ],
        jclx: [
          { required: true, message: "检测类型不能为空", trigger: "change" }
        ],
        jcfs: [
          { required: true, message: "检测方式不能为空", trigger: "change" }
        ],
        cycleRange: [
          { required: true, message: "检测周期不能为空", trigger: "blur" }
        ],
        jcrq: [
          { required: true, message: "检测日期不能为空", trigger: "blur" }
        ],
        jcjg: [
          { required: true, message: "检测结果不能为空", trigger: "change" }
        ],
        jcdd: [
          { required: true, message: "检测地点不能为空", trigger: "blur" }
        ],
        jcbg: [
          { required: true, message: "检测报告不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  computed: {
    currentPos: {
      get() {
        return [this.form.lng, this.form.lat]
      },
      set(newV) {
        const [lng, lat] = newV
        this.form.lng = lng
        this.form.lat = lat
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleUserChange(userId) {
      this.loadJcPlans(userId)
    },
    loadJcPlans(userId) {
      this.plans = []
      if (userId) {
        getJcPlan(userId).then(res=> {
          this.plans = res.data
        });
      }
    },
    getDetailAddress(detailAddress) {
      this.form.jcdd = detailAddress
    },
    /** 查询检测计划列表 */
    getList() {
      this.loading = true;
      this.handleWhenInSide((userId)=> {
        this.queryParams.userId = userId
      })

      listJcjh(this.queryParams).then(response => {
        this.jcjhList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jcjhId: undefined,
        userId: undefined,
        jcr: undefined,
        jclx: undefined,
        jcfs: undefined,
        jcrq: undefined,
        cycleRange: undefined,
        jcjg: undefined,
        jcdd: undefined,
        jcbg: undefined,
        sp: undefined,
        fj: undefined,
        lat: undefined,
        lng: undefined,
        remark: undefined,
        status: undefined,
        spr: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jcjhId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检测计划";
      this.handleWhenInSide((userId)=> {
        this.form.userId = userId
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const jcjhId = row.jcjhId || this.ids
      getJcjh(jcjhId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.loadJcPlans(this.form.userId)
        this.open = true;
        this.title = "修改检测计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.jcjhId != null) {
            updateJcjh(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addJcjh(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jcjhIds = row.jcjhId || this.ids;
      this.$modal.confirm('是否确认删除检测计划编号为"' + jcjhIds + '"的数据项？').then(() => {
        this.loading = true;
        return delJcjh(jcjhIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('event/jcjh/export', {
        ...this.queryParams
      }, `jcjh_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
